package org.scboy.dbms.service.imp;

import java.util.List;
import java.util.Map;

import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.dbms.dao.BaseAppPermissionDao;
import org.scboy.dbms.service.BaseAppPermissionService;
import org.scboy.utils.entitys.coobi.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.hutool.crypto.digest.MD5;

@Service
public class BaseAppPermissionServiceImpl implements BaseAppPermissionService {

	@Autowired
	EntityFactory factory;
	
	@Override
	public void updatePermission(List<Map<String, String>> permissions, String appName) {

		BaseAppPermissionDao permissionDao=factory.getDao(BaseAppPermissionDao.class);
		
		
		//更新权限配置单
		for (Map<String, String> permission : permissions) {
			
			//检查权限配置单是否存在
			String permissionId=MD5.create().digestHex(appName+"|"+permission.get("persmissionuUrl"));
			if(permissionDao.count(Model.create().add("id", permissionId))==0) {
				permissionDao.insert(
						Model.create()
						.add("id", permissionId)
						.add("appName", appName)
						.add("note", permission.get("note"))
						.add("httpMethod", permission.get("httpMethod"))
						.add("persmissionName", permission.get("persmissionName"))
						.add("persmissionuUrl", permission.get("persmissionuUrl"))
				);
			}else {
				permissionDao.update(
						Model.create()
						.add("id", permissionId)
						.add("appName", appName)
						.add("httpMethod", permission.get("httpMethod"))
						.add("note", permission.get("note"))
						.add("persmissionName", permission.get("persmissionName"))
						.add("persmissionuUrl", permission.get("persmissionuUrl"))
				);
			}
		}
	}
}
